package com.tuo51.evaluate.controller;

import com.tuo51.evaluate.service.StaffSettingService;
import com.tuo51.evaluate.vo.StaffSettingVO;
import com.tuo51.foudation.exception.BusinessException;
import com.tuo51.foudation.model.ErrorCode;
import com.tuo51.foudation.model.MemVerificationCache;
import com.tuo51.foudation.model.StatusResponse;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.tuo51.foudation.constant.RequestConstant.TICKET;

@RestController
@RequestMapping("/eapi/setting/")
@Api(value = "/eapi/setting/", tags = "测评 - 用户配置")
public class StaffSettingController {

    @Autowired
    private StaffSettingService settingService;

    @Autowired
    MemVerificationCache cache;

    @PostMapping("set")
    public StatusResponse save(@RequestHeader(TICKET) String ticket, StaffSettingVO entityVo) {
        String staffId = cache.getStaffId(ticket);

        if (StringUtils.isEmpty(entityVo.getKey())){
            throw new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK,"参数不能为空");
        }

        return StatusResponse.success(settingService.setSetting(staffId, entityVo.getKey(), entityVo.getValue(), entityVo.getExt()));
    }

    @GetMapping("get")
    public StatusResponse setting(@RequestHeader(TICKET) String ticket, String key ) {
        String staffId = cache.getStaffId(ticket);

        if (StringUtils.isEmpty(key)){
            throw new BusinessException(ErrorCode.PARAM_CAN_NOT_BLANK,"参数不能为空");
        }

        return StatusResponse.success(settingService.getSetting(staffId, key));
    }
}
